﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using NewDDSys.Entity;
using NewDDSys.IBLL;
using NewDDSys.IDAL;
using NewDDSys.Factory;
using NewDDSys.Utility;
using NewDDSys.DATAOUT;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Web_GZLTJ : System.Web.UI.Page
{
    private string ku = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["用户信息.岗位"].ToString() == "作业长" || Session["用户信息.岗位"].ToString() == "统计")
        {
            ku = Session["用户信息.部门"].ToString();

        }
        else
        {
            Label4.Visible = true;
            ddlKu.Visible = true;
        }
    }
    protected void btnZ_Click(object sender, EventArgs e)
    {
        if (Session["用户信息.岗位"].ToString() == "作业长" && Session["用户信息.岗位"].ToString() == "统计")
            ku = ddlKu.SelectedValue.ToString();
        ExcelEdit report = new ExcelEdit();
        report.CreateExcel();
        report.CreateWorkSheet("作业区工作量统计");
        report.CellsUnite(1, 1, 1, 6);
        report.WriteData("作业区工作量统计", 1, 1);
        report.CellsAlignment(1, 1, 1, 6, ExcelEdit.ExcelHAlign.居中, ExcelEdit.ExcelVAlign.居中);
        string kday = txtSday.Text.ToString();
        string t1 = Convert.ToDateTime(kday).AddHours(20).ToString();
        string eday = txtEday.Text.ToString();
        string t2 = Convert.ToDateTime(eday).AddHours(20).ToString();
        report.WriteData("开始日期", 2, 1);
        report.WriteData("结束日期", 2, 2);
        report.WriteData("库区", 2, 3);
        report.WriteData("作业内容", 2, 4);
        report.WriteData("车数", 2, 5);
        report.WriteData("合计吨位", 2, 6);
        string[] NR = { "外发", "倒库", "返厂", "倒装", "零提" };
        System.Data.DataTable dtResult = MakeNamesTable();
        string zcs = "";
        int cs = 0;
        string zdun = "";
        decimal dun = 0;
        string ycs = "";
        string ydun = "";
        string wcs = "";
        string wdun = "";
        for (int i = 3; i <= 7; i++)
        {
            report.WriteData(kday, i, 1);
            report.WriteData(eday, i, 2);
            report.WriteData(ku, i, 3);
            report.WriteData(NR[i-3], i, 4);
            DataSet ds = BLLFactory.CreateWorkBLL().ZYQTJ(kday, eday, ku, NR[i-3]);
            zcs = ds.Tables[0].Rows[0]["cs"].ToString();
            if (zcs != "")
                cs = Convert.ToInt32(zcs);
            zdun = ds.Tables[0].Rows[0]["ds"].ToString();
            if (zdun != "")
                dun = Convert.ToDecimal(zdun);
            DataSet ds1 = BLLFactory.CreateDxcBLL().NRWCL(t1, ku, NR[i - 3]);
            ycs = ds1.Tables[0].Rows[0]["csw"].ToString();
            if (ycs != "")
                cs = cs + Convert.ToInt32(ycs);
            ydun = ds1.Tables[0].Rows[0]["dunw"].ToString();
            if (ydun != "")
                dun = dun + Convert.ToDecimal(ydun);
            DataSet ds2 = BLLFactory.CreateDxcBLL().NRWCL(t2, ku, NR[i - 3]);
            wcs = ds2.Tables[0].Rows[0]["csw"].ToString();
            if (wcs != "")
                cs = cs - Convert.ToInt32(wcs);
            wdun = ds2.Tables[0].Rows[0]["dunw"].ToString();
            if (wdun != "")
                dun = dun - Convert.ToDecimal(wdun);

            System.Data.DataRow dtRow = dtResult.NewRow();
            dtRow[0] = cs.ToString();
            dtRow[1] = dun.ToString("f2");
            cs = 0;
            dun = 0;

            dtResult.Rows.Add(dtRow);
        }
        report.WriteData(dtResult, 3, 5);
        string title = Session["用户信息.姓名"].ToString() + DateTime.Now.ToString("yyyyMMdd").Trim() + "作业区工作量统计";
        string savepath = "zyqk\\" + title + ".xls";
        string RepPath = Server.MapPath(savepath);

        report.SaveAs(RepPath); //文档路径
        report.Close();
        string filename = title + ".xls";

        string spath = "zyqk\\" + filename;
        string RPath = Server.MapPath(spath);
        FileInfo f = new FileInfo(RPath);
        if (f.Exists)
        {

            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
            Response.ContentType = "application/msexcel";
            Response.WriteFile(RPath);
            Response.Flush();
            Response.Close();
            f.Delete();
        }
        
    }
    protected void btnB_Click(object sender, EventArgs e)
    {
        if (Session["用户信息.岗位"].ToString() == "作业长" && Session["用户信息.岗位"].ToString() == "统计")
            ku = ddlKu.SelectedValue.ToString();
        ExcelEdit report = new ExcelEdit();
        report.CreateExcel();
        report.CreateWorkSheet("班组工作量统计");
        report.CellsUnite(1, 1, 1, 7);
        report.WriteData("班组工作量统计", 1, 1);
        report.CellsAlignment(1, 1, 1, 7, ExcelEdit.ExcelHAlign.居中, ExcelEdit.ExcelVAlign.居中);
        string kday = txtSday.Text.ToString();
        string t1 = Convert.ToDateTime(kday).AddHours(20).ToString();
        string eday = txtEday.Text.ToString();
        string t2 = Convert.ToDateTime(eday).AddHours(20).ToString();
        report.WriteData("开始日期", 2, 1);
        report.WriteData("结束日期", 2, 2);
        report.WriteData("库区", 2, 3);
        report.WriteData("班组", 2, 4);
        report.WriteData("作业内容", 2, 5);
        report.WriteData("车数", 2, 6);
        report.WriteData("合计吨位", 2, 7);
        string[] NR = { "外发", "倒库", "返厂", "倒装", "零提" };
        string[] BAN = { "甲", "乙", "丙", "丁"};

        System.Data.DataTable dtResult = MakeNamesTable();
        string zcs = "";
        int cs = 0;
        string zdun = "";
        decimal dun = 0;
        string ycs = "";
        string ydun = "";
        string wcs = "";
        string wdun = "";
        int i = 3;
        for (int j = 0; j <= 3; j++)
        {
            for (int k = 0; k <= 4; k++)
            {
                report.WriteData(kday, i, 1);
                report.WriteData(eday, i, 2);
                report.WriteData(ku, i, 3);
                report.WriteData(BAN[j], i, 4);
                report.WriteData(NR[k], i, 5);
                DataSet ds = BLLFactory.CreateWorkBLL().BTJ(kday, eday, ku, BAN[j], NR[k]);
                zcs = ds.Tables[0].Rows[0]["cs"].ToString();
                if (zcs != "")
                    cs = Convert.ToInt32(zcs);
                zdun = ds.Tables[0].Rows[0]["ds"].ToString();
                if (zdun != "")
                    dun = Convert.ToDecimal(zdun);
                DataSet ds1 = BLLFactory.CreateDxcBLL().BanYCL(kday, eday, ku, NR[k], BAN[j]);
                ycs = ds1.Tables[0].Rows[0]["ycs"].ToString();
                if (ycs != "")
                    cs = cs + Convert.ToInt32(ycs);
                ydun = ds1.Tables[0].Rows[0]["yds"].ToString();
                if (ydun != "")
                    dun = dun + Convert.ToDecimal(ydun);
                DataSet ds2 = BLLFactory.CreateDxcBLL().BanWCL(kday, eday, ku, NR[k], BAN[j]);
                wcs = ds2.Tables[0].Rows[0]["wcs"].ToString();
                if (wcs != "")
                    cs = cs - Convert.ToInt32(wcs);
                wdun = ds2.Tables[0].Rows[0]["wds"].ToString();
                if (wdun != "")
                    dun = dun - Convert.ToDecimal(wdun);

                System.Data.DataRow dtRow = dtResult.NewRow();
                dtRow[0] = cs.ToString();
                dtRow[1] = dun.ToString("f2");
                dtResult.Rows.Add(dtRow);
                i++;

            }
            
        }
            
        report.WriteData(dtResult, 3, 6);
        string title = Session["用户信息.姓名"].ToString() + DateTime.Now.ToString("yyyyMMdd").Trim() + "班组工作量统计";
        string savepath = "zyqk\\" + title + ".xls";
        string RepPath = Server.MapPath(savepath);

        report.SaveAs(RepPath); //文档路径
        report.Close();
        string filename = title + ".xls";

        string spath = "zyqk\\" + filename;
        string RPath = Server.MapPath(spath);
        FileInfo f = new FileInfo(RPath);
        if (f.Exists)
        {

            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
            Response.ContentType = "application/msexcel";
            Response.WriteFile(RPath);
            Response.Flush();
            Response.Close();
            f.Delete();
        }
        
    }
    private System.Data.DataTable MakeNamesTable()
    {
        // Create a new DataTable titled 'Names.'
        System.Data.DataTable namesTable = new System.Data.DataTable("tb");

        // Add three column objects to the table.
        DataColumn csColumn = new DataColumn();
        csColumn.DataType = System.Type.GetType("System.String");


        namesTable.Columns.Add(csColumn);

        DataColumn dunColumn = new DataColumn();
        dunColumn.DataType = System.Type.GetType("System.String");


        namesTable.Columns.Add(dunColumn);

        // Return the new DataTable.
        return namesTable;
    }
    
}
